$(function() {
	$("#dataTable").jqxDataTable({
		width : "100%",
		source : getSource(),
		pageable : true,
		serverProcessing : true,
		pageSize : 18,
		pagerButtonsCount : 10,
		columnsResize : true,
		sortable : true,
		columns : [ {
			text : "序号",
			width : 50,
			cellsRenderer : function(row) {
				return (row + 1).toString();
			}
		}, {
			text : "商家名称",
			dataField : "shop_name"
		}, {
			text : "商品名称",
			dataField : "goods_name",
			width : "35%"
		}, {
			text : "封条编码",
			dataField : "number",
			width : "15%"
		}, {
			text : "省份",
			dataField : "province",
			width : "10%"
		}, {
			text : "城市",
			dataField : "city",
			width : "10%"
		}, {
			text : "地区",
			dataField : "district",
			width : "10%"
		} ],
	});

	$("#dataTable").on("rowDoubleClick", function(info, args) {
		if (args && args.flag) {
			info = args;
		}
		editingIndex = info.args.index;
		editingData = info.args.row;

		$.ajax({
			type : "GET",
			dataType : "json",
			url : "/manager/barCode/getShopList.do",
			success : function(result) {
				if (result.code == 1) {
					var option = new Array();
					option.push('<option value="0">请选择商家</option>');
					$.each(result.data, function(i, val) {
						option.push('<option value="' + val.uuid + '">');
						option.push(val.name);
						option.push('</option>');
					});
					buildEditer(option.join(''));
				}
			},
			error : function() {
			}
		});

	});

	/**
	 * 搜索
	 */
	$("#search").click(function() {
		var shop_name = $("#shop_name").val();
		var codeNumber = $("#codeNumber").val();
		var json = {
			shop_name : shop_name,
			number : codeNumber
		}
		$("#dataTable").jqxDataTable({
			source : getSource(json)
		});
	});

});

/**
 * 获取数据源
 */
function getSource(json) {
	var url = "/manager/barCode/getListBySelective.do?test=1";
	if (json !== undefined) {
		if (json.shop_name !== undefined && json.shop_name !== null
				&& json.shop_name !== "") {
			url += "&shop_name=" + json.shop_name;
		}
		if (json.number !== undefined && json.number !== null
				&& json.number !== "") {
			url += "&number=" + json.number;
		}
	}
	// 设置数据源
	var tableSource = new $.jqx.dataAdapter({
		dataType : "json",
		dataFields : [ {
			name : "id",
			type : "int"
		}, {
			name : "uuid",
			type : "string"
		}, {
			name : "shop_uuid",
			type : "string"
		}, {
			name : "shop_name",
			type : "string"
		}, {
			name : "goods_uuid",
			type : "string"
		}, {
			name : "goods_name",
			type : "string"
		}, {
			name : "picture",
			type : "string"
		}, {
			name : "number",
			type : "string"
		}, {
			name : "province",
			type : "string"
		}, {
			name : "city",
			type : "string"
		}, {
			name : "district",
			type : "string"
		} ],
		id : "id",
		url : url
	});
	return tableSource;
}

function buildEditer(option) {
	var contentDiv = new Array();
	contentDiv.push('<form id="barCode-editer" class="form" method="post"');
	contentDiv
			.push('					enctype="multipart/form-data" style="height: 100%; overflow: auto;">');
	contentDiv.push('<input type="hidden" id="uuid" name="uuid" /><input type="hidden" id="goods_uuid" name="goods_uuid" />');
	contentDiv
			.push('			<input type="hidden" id="goods_name" name="goods_name" /> <table>');
	contentDiv
			.push('					<tr><td><label	class="from-label" for="name">选择商家：</label></td> <td><select id="shop_uuid"');
	contentDiv.push('						name="shop_uuid" class="form-control" size="1">');
	contentDiv.push(option);
	contentDiv.push('					</select></td></tr> ');
	contentDiv
			.push('<tr><td><label	class="from-label" for="name">选择商品：</label></td><td><div id="combobox"></div></td></tr>');
	contentDiv
			.push('<tr><td><label class="from-label" for="contacts">封条编码：</label></td> <td id="numberTd"><input');

	contentDiv
			.push('						type="text" id="number" name="number" class="form-control"');
	contentDiv.push('						placeholder="封条编码" /></td></tr>');
	contentDiv
			.push(' <tr><td><label class="from-label" for="photo">条码图片：</label></td>');
	contentDiv
			.push('<td id="photoTd"><div class="fileupload fileupload-new data-provides" style="display:inline-block;vertical-align: middle;">');
	contentDiv
			.push('<div class="form-control fileupload-new thumbnail" style="width: 200px; height: 150px;">');
	contentDiv.push('<img id="photo22" name="photo22" />');
	contentDiv.push('</div>');
	contentDiv
			.push('<div class="form-control fileupload-preview fileupload-exists thumbnail" style="width: 200px; height: 150px;"></div>');
	contentDiv.push('<div id="uploadimg">');
	contentDiv
			.push('<input type="file" name="photo" id="photo" style="display:none;" />');
	contentDiv
			.push('<label for="photo" class="fileupload-new btn btn-sm btn-success" style="margin:0">选择文件</label>');
	contentDiv
			.push('<label for="photo" class="fileupload-exists btn btn-sm btn-success" style="margin:0">更改</label>');
	contentDiv
			.push('<span  class="fileupload-exists btn btn-sm btn-danger" data-dismiss="fileupload" onclick="removePicture(this)">移除</span>');
	contentDiv.push('</div>');
	contentDiv.push('</div></td></tr></table>');
	contentDiv.push('					<div class="form"');
	contentDiv
			.push('						style="position: absolute; bottom: 0px; width: 100%; text-align: right; background-color: #DDD;">');
	contentDiv
			.push('						<input type="button" class="btn btn-sm btn-success"');
	contentDiv
			.push('							onclick="save(this)" value="保存" /> <input type="button"');
	contentDiv
			.push('							class="btn btn-sm btn-info" onclick="closeEditer(this)"');
	contentDiv.push('							value="返回" />');
	contentDiv.push('					</div>');
	contentDiv.push('				</form>');

	_editerIndex = layer.open({
		id : "sellerEditer",
		title : "封条信息录入",
		type : 1,
		skin : 'layui-layer-rim',
		area : [ '700px', '450px' ],
		content : contentDiv.join(""),
		success : function(e) {
			$contentDiv = $(e);
			$contentDiv.find("#shop_uuid").on("change", onChangeEven);
		}
	});
	$("#combobox").jqxComboBox({
		selectedIndex : 0,
		// source : getSource2(""),
		displayMember : "name",
		valueMember : "uuid",
		width : 200,
		height : 25
	});
	var rows = $("#dataTable").jqxDataTable("getSelection");
	if (rows.length > 0) {
		_SelectRow = rows[0];
		$contentDiv.find("#id").val(rows[0].id);
		$contentDiv.find("#uuid").val(rows[0].uuid);
		$contentDiv.find("#shop_uuid").val(rows[0].shop_uuid);
		$contentDiv.find("#number").val(rows[0].number);
		$("#photo22").attr("src", "http://www.t10mall.com" + rows[0].picture);
		var items = $("#combobox").jqxComboBox('getVisibleItems');
		$("#combobox").jqxComboBox({
			source : getSource2(rows[0].shop_uuid)
		});
		$("#combobox").jqxComboBox('val', rows[0].goods_uuid);
	} else {
		_SelectRow = null;
		$("#combobox").jqxComboBox('uncheckAll');
	}

}

function save(obj) {
	var $barCode = $(obj).parents('form');
	var $combobox = $barCode.find("#combobox");
	var shop_uuid = $("#shop_uuid").val();
	if (shop_uuid === "0" || shop_uuid === 0 || shop_uuid === undefined
			|| shop_uuid === null) {
		layer.tips('请选择商家', '#shop_uuid');
		return;
	}
	var item = $combobox.jqxComboBox('getSelectedItem');
	if (item === null || item === undefined) {
		layer.tips('请选择商品', '#combobox');
		return;
	}
	var number = $("#number").val();
	if (number === "" || number === null || number === undefined) {
		layer.tips('请输入编码', '#numberTd');
		return;
	}
	var photo = $("#photo").val();
	if (photo === "" || photo === null || photo === undefined
			|| photo.lenghth == 0) {
		layer.tips('请选择图片', '#photoTd');
		return;
	}
	$("#goods_uuid").val(item.value);
	$("#goods_name").val(item.label);
	$barCode.ajaxSubmit({
		url : "/manager/barCode/edit.do",
		type : "POST",
		dataType : "JSON",
		beforeSubmit : function() {
		},
		success : function(result) {
			layer.msg(result.msg);
			if (_SelectRow == null) {
				$("#dataTable").jqxDataTable('addRow', result.data.uuid,
						result.data);
				layer.close(_editerIndex);
			} else {
				_SelectRow.shop_uuid = result.data.shop_uuid;
				_SelectRow.number = result.data.number;
				updateRow(null, _SelectRow);
			}
		},
		error : function(XMLResponse) {
			layer.msg('操作失败！');
			// layer.msg(XMLResponse.responseText);
		}
	});
}

function onChangeEven() {
	$("#combobox").jqxComboBox({
		source : getSource2($(this).val()),
	});
}

function getSource2(shop_uuid) {
	var url = "/manager/barCode/getComboList.do";
	if (shop_uuid !== undefined && shop_uuid !== null) {
		url += "?shop_uuid=" + shop_uuid;
	}
	var source = {
		datatype : "json",
		datafields : [ {
			name : 'uuid'
		}, {
			name : 'name'
		} ],
		url : url,
		async : false
	};
	return new $.jqx.dataAdapter(source);
}
/**
 * 更新行
 * 
 * @param index
 * @param row
 */
function updateRow(index, row) {
	if (index == null) {
		var rows = $("#dataTable").jqxDataTable('getView');
		for (i = rows.length - 1; i >= 0; i--) {
			if (row.uuid === rows[i].uuid) {
				index = i;
			}
		}
	}
	$("#dataTable").jqxDataTable('updateRow', index, row);
	layer.close(_editerIndex);
}

function removePicture() {
	$(".fileupload").fileupload("clear");
}